package com.alex.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.stereotype.Controller;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    private static final Contact DEFAULT_CONTACT = new Contact("alex", "alex.com", "alex@google.com");

    @Value("${spring.profiles.active}")
    private static String profile;

    @Bean
    public Docket docket(Environment env) {
        System.out.println(profile);
        String profile2 = env.getProperty("spring.profiles.active");
        System.out.println(profile2);

        boolean acceptsProfiles = env.acceptsProfiles(Profiles.of("dev", "test"));

        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())

//                .enable(true)
                .enable(acceptsProfiles)

                .groupName("hello")

                .select()

//                .apis(RequestHandlerSelectors.any())
//                .apis(RequestHandlerSelectors.none())
//                .apis(RequestHandlerSelectors.basePackage("com.alex.controller"))
//                .apis(RequestHandlerSelectors.withMethodAnnotation(GetMapping.class))
                .apis(RequestHandlerSelectors.withClassAnnotation(Controller.class))

//                .paths(PathSelectors.any())
//                .paths(PathSelectors.regex(""))
//                .paths(PathSelectors.ant("/alex/controller/**"))

                .build();
    }

    @Bean
    public Docket docket01() {
        return new Docket(DocumentationType.SPRING_WEB).groupName("world");
    }

    @Bean
    public Docket docket02() {
        return new Docket(DocumentationType.SPRING_WEB).groupName("ticket");
    }

    @Bean
    public Docket docket03() {
        return new Docket(DocumentationType.SPRING_WEB).groupName("market");
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "alex Api Documentation",
                "my Api Documentation",
                "3.1.6",
                "urn:alex", DEFAULT_CONTACT,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}
